package com.athenaeum.dataxplore.controller;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.athenaeum.datastore.domain.Project;
import com.athenaeum.datastore.domain.User;
import com.athenaeum.datastore.service.UserService;
import com.athenaeum.datastore.util.UserUtil;

@Controller
public class ProjectController {

	private static final Logger log = LoggerFactory
			.getLogger(ProjectController.class);
	@Autowired
	private UserService userService;

	@RequestMapping(value = "/project/browse/{abbr}", method = RequestMethod.GET)
	public String index(Locale locale, Model model, @PathVariable String abbr) {
		log.info("Loading Project - Index Page... abbr - " + abbr);
		User sessionUser = userService.findOne(UserUtil.getUsernameInContext());
		for (Project project : sessionUser.getProjects().values()) {
			if (project.getAbbr().equalsIgnoreCase(abbr)) {
				User user = userService.findOne(project.getLead());
				Map<String, Object> projectAttributes = new HashMap<String, Object>();
				projectAttributes.put("fullName", user.getFullName());
				project.setAttributes(projectAttributes);
				model.addAttribute("currentProject", project);
				break;
			}
		}
		return "project/browse";
	}
}
